package com.xingji.caixia.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;

import com.xingji.caixia.Constant;
import com.xingji.caixia.R;
import com.xingji.caixia.adapter.DataAdapter;
import com.xingji.caixia.entitys.CardDao;
import com.xingji.caixia.entitys.UploadData;
import com.xingji.caixia.entitys.dataType;
import com.zhy.android.percent.support.PercentLinearLayout;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.ex.DbException;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.ExecutorService;

@ContentView(R.layout.fragment_data)
public class DataFragment extends BaseFragment implements DataAdapter.OnItemclicklintener {

    @ViewInject(R.id.recycleview)
    RecyclerView recyleview;

    @ViewInject(R.id.frame_data)
    FrameLayout frame_data;

    @ViewInject(R.id.linearLayout3)
    PercentLinearLayout linearLayout3;

    DataAdapter adapter;
    List<UploadData> datalist;
    ImageView imageView;
    int ScreenW ,ScreenH;


    private static final String SCHOOL_ID = "school_id";
    private String school_id;

    private OnFragmentInteractionListener mListener;
    private Bitmap bp;

    public static DataFragment newInstance(String arg) {
        DataFragment fragment = new DataFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            school_id = getArguments().getString(SCHOOL_ID);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return x.view().inject(this,inflater,container);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    @Override
    public void onResume() {
        super.onResume();
        refreshUI();
    }

    public void refreshUI(){
        Log.e("refreshUI","refreshUI");
        try {
            datalist = CardDao.getCards(dataType.UNPLOAD);
            adapter.refresh(datalist);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    private void initView(){

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        recyleview.setLayoutManager(linearLayoutManager);
        datalist = new ArrayList<>();
        adapter = new DataAdapter(getActivity(), datalist);
        recyleview.setAdapter(adapter);
        recyleview.setItemAnimator(new DefaultItemAnimator());
//        //// TODO: 2017/9/28 ITEM无视点击
        adapter.setOnItemclickLintener(this);

        imageView = new ImageView(getActivity());
//        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setVisibility(View.GONE);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (bp!=null) bp.recycle();
                imageView.setVisibility(View.GONE);
            }
        });
        DisplayMetrics dm = new DisplayMetrics();//创建矩阵
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        frame_data.addView(imageView);

        ScreenW=dm.widthPixels; //得到屏幕的宽度
        ScreenH=dm.heightPixels; //得到屏幕的高度

    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public void Itemclick(View view, int position) {
        UploadData data = datalist.get(position);
        bp = BitmapFactory.decodeFile(data.getImgPath());
        imageView.setImageBitmap(bp);
        imageView.setVisibility(View.VISIBLE);
        String msg ;
        if (data.getUploaded()){
            msg = data.getResult();
        }else{
            msg = "此卡未上传成功或未开始上传...";
        }
        Toast.makeText(getActivity(),msg,Toast.LENGTH_SHORT).show();
    }


    public interface OnFragmentInteractionListener {
        void onFragmentInteraction(Uri uri);
    }
}
